package com.beetlestech.nfe.main.biz.com.impl;


import java.util.ArrayList;
import java.util.List;

import com.beetlestech.nfe.fw.core.biz.AbstractDBLogic;
import com.beetlestech.nfe.fw.core.util.StringUtil;
import com.beetlestech.nfe.main.biz.com.HbVideo;
import com.beetlestech.nfe.main.biz.com.dto.MstfileInfoDTO;
import com.beetlestech.nfe.main.biz.constant.CommonConstant;
import com.beetlestech.nfe.main.biz.constant.CtgCodeConstant;
import com.beetlestech.nfe.main.biz.constant.HbMessageCode;
import com.beetlestech.nfe.main.db.base.StatementDTO;
import com.beetlestech.nfe.main.db.base.StatementUtil;
import com.beetlestech.nfe.main.db.constant.SQLConstant;
import com.beetlestech.nfe.main.db.tbl.MstcatagoryDTO;
import com.beetlestech.nfe.main.db.tbl.MstcatagorycDTO;
import com.beetlestech.nfe.main.db.tbl.MstfileDTO;




public class HbVideoImpl extends AbstractDBLogic implements HbVideo, CtgCodeConstant, HbMessageCode, CommonConstant, SQLConstant {
	protected static final String SH_INFO = "折扣信息表";
	
	//取出所有活动
	@Override
	public  MstfileInfoDTO[] getMstfileInfoList(String vName,Integer coursetypeC,Integer year){
		MstfileDTO ms=new MstfileDTO();
		
		StatementDTO statementDTO = new StatementDTO();
		if(vName==null || vName.equals("") && coursetypeC==0 && year==0){
			StatementUtil.andWhere(statementDTO, MstfileDTO.COLUMN_TYPE, StatementUtil.KEYWORD.EQUAL,1);
		}
		
		
		if(vName!=null && !vName.equals("")){
			StatementUtil.andWhere(statementDTO, MstfileDTO.COLUMN_FILE_NAME, StatementUtil.KEYWORD.LIKE ,vName);
			StatementUtil.andWhere(statementDTO, MstfileDTO.COLUMN_TYPE, StatementUtil.KEYWORD.EQUAL,1);
		}
		if(coursetypeC!=0){
			StatementUtil.andWhere(statementDTO, MstfileDTO.COLUMN_COURSETYPE_C, StatementUtil.KEYWORD.EQUAL ,coursetypeC);
			StatementUtil.andWhere(statementDTO, MstfileDTO.COLUMN_TYPE, StatementUtil.KEYWORD.EQUAL,1);
		}
		if(year!=0){
			StatementUtil.andWhere(statementDTO, MstfileDTO.COLUMN_YEAR, StatementUtil.KEYWORD.EQUAL ,year);
			StatementUtil.andWhere(statementDTO, MstfileDTO.COLUMN_TYPE, StatementUtil.KEYWORD.EQUAL,1);
		}
	
		ms.set_dynamicWhereCondition(statementDTO.getSQLStatement());
		
		List<MstfileDTO> listd= queryDAO.queryForObjectList(MstfileDTO.SQL_SEARCH, ms);
		List<MstfileInfoDTO> listm=new ArrayList<MstfileInfoDTO>();
		for(int i=0;i<listd.size();i++){
			MstfileInfoDTO mm=new MstfileInfoDTO();
			mm.setfileid(listd.get(i).getfileid());
			mm.setfilename(listd.get(i).getfilename());
			mm.setpath(listd.get(i).getpath());
			mm.setcoursetypec(listd.get(i).getcoursetypec());
			//对课程分类id进行转化
			MstcatagoryDTO mc=new MstcatagoryDTO();
			Integer num=listd.get(i).getcoursetypec();
			if(num==null||num==0){
				mm.setCname("");
			}else{
				mc.setId(num);
				mc=queryDAO.queryForObject(MstcatagoryDTO.EQUAL_SEARCH, mc);
				mm.setCname(mc.getName());
			}
			mm.setyear(listd.get(i).getyear());
			listm.add(mm);
		}
		MstfileInfoDTO[] arr = (MstfileInfoDTO[])listm.toArray(new MstfileInfoDTO[listm.size()]);
		
		return arr;
	}
	//删除视频
	@Override
	public void deleteVideo(String actionType,String acSql,Integer vid){
		if(actionType.equals("single")){
			MstfileDTO mDTO =new MstfileDTO();
			if(vid==null){
				return ;
			}
			mDTO.setfileid(vid);
			updateDAO.updateOne(MstfileDTO.DELETE, mDTO, M10190WS, SH_INFO);
			
		}else if(actionType.equals("many")){
			String[] dbSqlArray=acSql.split(",");
			for (int i = 0; i < dbSqlArray.length; i++) {
				if(StringUtil.isEmpty(dbSqlArray[i])){
					break;
				}
				MstfileDTO mDTO =new MstfileDTO();
				mDTO.setfileid(Integer.parseInt(dbSqlArray[i]));
				updateDAO.updateOne(MstfileDTO.DELETE, mDTO, M10190WS, SH_INFO);
			}
		}
	}

	//修改视频
	@Override
	public  void updateVideo(Integer vId,String vname,String vpath,Integer coursetypeC,Integer year){
		MstfileDTO ma=new MstfileDTO();
		ma.setfileid(vId);
		ma=queryDAO.queryForObject(MstfileDTO.EQUAL_SEARCH, ma);
		
		ma.setfilename(vname);
		ma.setpath(vpath);
		ma.setcoursetypec(coursetypeC);
		ma.setyear(year);
		updateDAO.updateOne(MstfileDTO.UPDATE, ma, M10190WS, SH_INFO);
	}
	
	//根据id 查询视频
	@Override
	public MstfileInfoDTO getVideoInfo(Integer vId){
		MstfileDTO ma=new MstfileDTO();
		ma.setfileid(vId);
		ma=queryDAO.queryForObject(MstfileDTO.EQUAL_SEARCH, ma);
		MstfileInfoDTO mai=new MstfileInfoDTO();
		mai.setfileid(vId);
		mai.setfilename(ma.getfilename());
		mai.setpath(ma.getpath());
		mai.setcoursetypec(ma.getcoursetypec());
		mai.setyear(ma.getyear());
		
		return mai;
	}
	
}
